﻿-- there's probably a better way to handle this, but im too lazy to figure it out
DailyTrackerTimer = {};
DailyTrackerTimer.Count = 0;
DailyTrackerTimer.Timeouts = {};

function DailyTrackerTimer:OnUpdate(timePassed)
	DailyTrackerTimer.Count = DailyTrackerTimer.Count + timePassed;
	DailyTracker:Debug( "TIMER AT: " .. DailyTrackerTimer.Count );
	
	local newTimeouts = {};
	local signature;
	for i=1,table.getn(DailyTrackerTimer.Timeouts) do
		signature = DailyTrackerTimer.Timeouts[i];
		if signature.ExpiresAt <= DailyTrackerTimer.Count then
			if signature.Callback then
				signature.Callback( unpack(signature.Args) );
			end
		else
			table.insert( newTimeouts, signature );
		end
	end
	
	DailyTrackerTimer.Timeouts = newTimeouts;
	if table.getn(DailyTrackerTimer.Timeouts) == 0 then
		DailyTrackerTimer.Frame:Hide();
	end
end

function DailyTrackerTimer:Set(timeout, callback, ...)
	local callSignature = {};
	callSignature.ExpiresAt = self.Count + timeout;
	callSignature.Callback = callback;
	
	-- there's probably a better way to do this
	callSignature.Args = {};
	for i=1,select("#",...) do
		callSignature.Args[i] = select(i,...);
	end

	table.insert( DailyTrackerTimer.Timeouts, callSignature );
	self.Frame:Show();
end

DailyTrackerTimer.Frame = CreateFrame("Frame");
DailyTrackerTimer.Frame:SetScript("OnUpdate", DailyTrackerTimer.OnUpdate);
DailyTrackerTimer.Frame:Hide();
